package main;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 谢焕涛
 * @Description: InequalitySolver
 * @Date: 2025/5/13 上午9:10
 * @Version: 1.0
 */
public class InequalitySolver {

    // 参数配置类
    static class SolverConfig {
        double a;                   // 阈值 a
        int[] n;                   // 参数数组 n_i
        int[] u;                   // 参数数组 u_i
        int[] s;                   // 参数数组 s_i
        int[] xMin;                // x_i 的最小值
        Integer[] xMax;            // x_i 的最大值（可选）
    }

    public static void main(String[] args) {
        // 示例配置（需根据实际需求修改）
//        a > Σ(n * x / ( u/ s) )
//        1 * x /20  + 1 * y / 33  + 3 * z / 20 < 1 （ x>=7, y>=10, z>=1)
        SolverConfig config = new SolverConfig();
        config.a = 1;
        config.n = new int[]{1, 1, 3};      // n_i 数组
        config.u = new int[]{20, 33, 20};      // u_i 数组
        config.s = new int[]{1,1,1};      // s_i 数组
        config.xMin = new int[]{7,10, 1};   // x_i 最小值
        config.xMax = new Integer[]{null, null, null}; // x_i 最大值（设为 null 则不限制）

        // 求解并输出结果
        List<List<Integer>> solutions = solve(config);
        printSolutions(solutions, config);
    }

    // 核心求解逻辑（回溯算法）
    private static List<List<Integer>> solve(SolverConfig config) {
        List<List<Integer>> validCombinations = new ArrayList<>();
        backtrack(validCombinations, new ArrayList<>(), 0, config);
        return validCombinations;
    }

    private static void backtrack(
            List<List<Integer>> result,
            List<Integer> current,
            int index,
            SolverConfig config
    ) {
        if (index == config.n.length) {
            // 计算总和是否满足 a > Σ(n_i * x_i * s_i / u_i)
            double sum = 0.0;
            for (int i = 0; i < current.size(); i++) {
                sum += (config.n[i] * current.get(i) * config.s[i]) / (double) config.u[i];
            }
            if (sum < config.a) {
                result.add(new ArrayList<>(current));
            }
            return;
        }

        // 动态确定 x_i 的遍历范围
        int min = config.xMin[index];
        int max = (config.xMax != null && config.xMax[index] != null) ?
                config.xMax[index] : Integer.MAX_VALUE;

        for (int xi = min; xi <= max; xi++) {
            current.add(xi);
            backtrack(result, current, index + 1, config);
            current.remove(current.size() - 1);
        }
    }

    // 输出结果
    private static void printSolutions(List<List<Integer>> solutions, SolverConfig config) {
        System.out.println("满足条件的组合共有 " + solutions.size() + " 种：");
        for (List<Integer> combo : solutions) {
            StringBuilder sb = new StringBuilder("x = [");
            for (int i = 0; i < combo.size(); i++) {
                sb.append(combo.get(i));
                if (i < combo.size() - 1) sb.append(", ");
            }
            sb.append("], 计算值 = ");
            double sum = 0.0;
            for (int i = 0; i < combo.size(); i++) {
                sum += (config.n[i] * combo.get(i) * config.s[i]) / (double) config.u[i];
            }
            sb.append(String.format("%.2f", sum));
            System.out.println(sb);
        }
    }
}
